package ldh.im.base;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ConcurrentHashMap;

public class CacheUserManager {

    private static final Logger LOGGER = LoggerFactory.getLogger(CacheUserManager.class);
    private static CacheUserManager instance;

    private ConcurrentHashMap<Long, ConcurrentHashMap<String, Object>> userCacheMap = new ConcurrentHashMap<>();

    private CacheUserManager() {
    }

    public static CacheUserManager getInstance() {
        if (instance == null) {
            synchronized (CacheUserManager.class) {
                if (instance == null) {
                    instance = new CacheUserManager();
                }
            }
        }
        return instance;
    }

    public Object getCache(Long userId, String key) {
        ConcurrentHashMap<String, Object> dataMap = userCacheMap.get(userId);
        if (dataMap != null) {
            return dataMap.get(key);
        }
        return null;
    }

    public void setCache(Long userId, String key, Object value) {
        ConcurrentHashMap<String, Object> dataMap = userCacheMap.get(userId);
        if (dataMap == null) {
            dataMap = new ConcurrentHashMap<>();
            userCacheMap.putIfAbsent(userId, dataMap);
        }
        dataMap.put(key, value);
    }

}
